package day1.homework.one;

/**
 * @创建人 MBY381
 * @创建时间 2022/6/27 14:33
 * @描述 暂无
 */

public class BinaryLinkedList<T> {
    private Node<T> head;
    private Node<T> tail;

    public BinaryLinkedList() {
        super();
        head = null;
        tail = null;
    }


    //头插法，替换head
    public void insertHead(T value) {
        Node<T> node = new Node<>(value);
        if (this.isEmpty()) {
            tail = node;
        } else {
            head.setPre(node);
        }
        node.setNext(head);
        head = node;
    }

    //尾插法，替换tail
    public void insertTail(T value) {
        Node<T> node = new Node<>(value);
        if (this.isEmpty()) {
            head = node;
        } else {
            node.setPre(tail);
            tail.setNext(node);
        }
        tail = node;
    }

    //从头部开始遍历
    public void iterator() {
        System.out.print("[ ");
        Node<T> current = head;
        while (current != null) {
            System.out.print(current+",");
            current = current.getNext();
        }
        System.out.println(" ]");
    }


    //从尾部开始遍历
    public void reverseIterator() {
        System.out.print("[ ");
        Node<T> current = tail;
        while (current != null) {
            System.out.print(current+",");
            current = current.getPre();
        }
        System.out.println(" ]");
    }



    //根据索引节点插入新节点
    public void insertAfter(T key, T value) {
        Node<T> node = new Node<>(value);
        Node<T> current;
        current = head;
        while ((current != null) && (!current.getValue().equals(key))) {
            current = current.getNext();
        }

        if (current == null) {
            if (this.isEmpty()) {
                head = node;
            } else {
                tail.setNext(node);
                node.setPre(tail);
            }
            tail = node;
        } else {
            if (current == tail) {
                node.setNext(null);
                tail = node;
            } else {
                node.setNext(current.getNext());
                current.getNext().setPre(node);
            }
            current.setNext(node);
            node.setPre(current);

        }
    }




    /*
     * 非题目要求的方法 经测试运转正常
     */

    //检查链表是否为空
    public boolean isEmpty() {
        return (this.head == null);
    }


    //删除头结点
    public Node<T> deleteHead() {
        Node<T> temp = head;
        if (head.getNext() == null) {
            tail = null;
        } else {
            head.getNext().setPre(null);
        }
        head = head.getNext();

        return temp;
    }


    //删除尾结点
    public Node<T> deleteTail() {
        Node<T> temp = tail;
        if (head.getNext() == null) {
            head = null;
        } else {
            tail.getPre().setNext(null);
        }
        tail = tail.getPre();
        return temp;
    }


    //按值删除
    public Node<T> delete(T key) {
        Node<T> current = head;
        Node<T> temp=new Node<>(key);
        while (current != null && !current.equals(temp)) {
            current = current.getNext();
        }
        if (current == null) {
            return null;
        } else {
            if (current.equals(head)) {
                head = current.getNext();
                current.getNext().setPre(null);
            } else if (current.equals(tail) ) {
                tail = current.getPre();
                current.getPre().setNext(null);
            } else {
                current.getPre().setNext(current.getNext());
                current.getNext().setPre(current.getPre());
            }
        }
        return current;
    }



}